#include "Irc_Client.h"
#include "irc_sink_filter.h"

#include <iostream>
#include <iterator>
#include <string>

Irc_Client::Irc_Client( boost::asio::io_service &io, std::string ip, std::string port, std::string chan, std::string pass ) : socket( io )
{
	channel = chan;
	password = pass;
	manager = new irc_filter_manager();

	manager->chain( new irc_nick_filter( "d4rk", boost::lambda::bind( &Irc_Client::event_nick, this, boost::lambda::_1 ) ) )->chain( new irc_sink_filter() );
	
	boost::system::error_code ec = boost::asio::error::host_not_found;
	
	tcp::resolver resolver( io );
	tcp::resolver::query query( ip.c_str(), port.c_str() );

	tcp::resolver::iterator end_it2 = resolver.resolve( query );

	while( ec && !socket.connect( *ent_it2, ec ) )
	{
		socket.close();
		++end_it;
	}
}


Irc_Client::~Irc_Client(void)
{
	delete manager;
}

void Irc_Client::loop()
{
	boost::asio::streambuf data;

	while( 1 )
	{
		socket.read_until( data, "\r\n" );
		std::string line;  
		std::istream(&data) >> line;  

		std::cout << line;
	}
}